package com.xty.xtyle.mix;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.google.android.material.tabs.TabLayout;
import com.xty.xtyle.R;
import com.xty.xtyle.base.FmPagerAdapter;
import com.xty.xtyle.base.PicTabFragment;
import com.xty.xtyle.base.TabFragment;
import com.xty.xtyle.db.ClothTypes;
import com.xty.xtyle.db.ClothTypesDao;
import com.xty.xtyle.utils.CommonUtil;

import java.util.ArrayList;
import java.util.List;

public class MixPicActivity extends AppCompatActivity {


    private TabLayout tabLayout;
    private ViewPager viewPager;
    private FmPagerAdapter pagerAdapter;
    private List<Fragment> fragments = new ArrayList<>();
    private List<ClothTypes> types;
    //以下用来去挑选页面获得衣服,通过int状态码来监听结果
    public static final int PIC_STATE_BACK = 0x32;
    public static final int PIC_STATE_REQUEST = 0x31;
    public static final String PIC_STATE_POSITION = "position";

    //静态启动当前Activity页面
    public static void startActivity(Activity context, int position) {
        Intent intent = new Intent(context, MixPicActivity.class);
        intent.putExtra(PIC_STATE_POSITION, position);
        context.startActivityForResult(intent, PIC_STATE_REQUEST);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mix_pic);

        getTypes();

        init();
    }

    //获取衣服的所有类型
    void getTypes() {
        ClothTypesDao dao = new ClothTypesDao();
        types = dao.findAll();
    }
    private void init() {

        tabLayout = findViewById(R.id.tablayout);
        viewPager = findViewById(R.id.viewpager);
        //生成类型对应的导航栏,并传递选择图片的标志
        for(int i=0;i<types.size();i++){
            PicTabFragment tabFragment = new PicTabFragment();
            Bundle args = new Bundle();
            tabFragment.setCallBack(new PicTabFragment.PickCallBack() {
                @Override
                public void callBack(int clothId) {
                    Intent intent = new Intent();
                    intent.putExtra("id", clothId);
                    Intent origin = getIntent();
                    int pos = origin.getIntExtra(PIC_STATE_POSITION, 0);
                    intent.putExtra(PIC_STATE_POSITION, pos);
                    setResult(PIC_STATE_BACK, intent);
                    finish();
                }
            });
            args.putString("type", types.get(i).getType());
            tabFragment.setArguments(args);
            fragments.add(tabFragment);
            tabLayout.addTab(tabLayout.newTab());
        }

        tabLayout.setupWithViewPager(viewPager,false);
        pagerAdapter = new FmPagerAdapter(fragments, getSupportFragmentManager());
        viewPager.setAdapter(pagerAdapter);

        for(int i=0;i<types.size();i++){
            tabLayout.getTabAt(i).setText(types.get(i).getType());
        }
    }

}
